<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/18
 * Time: 18:18
 */

namespace App\Http\Classes\IM;

use App\Http\Classes\Common;

class IMCurl
{
    protected $ch;
    public $config = [
        'AppKey' => '4104d08c4c160cd92701c7fd8f88a273',
        'AppSecret' => '7a64f98f9074'
    ];

    //public $host = 'https://api.netease.im/nimserver/';


    protected $header = array();

    private $nonce;
    private $curTime;

    protected $response;
    protected $request;
    protected $body = '';

    public function __construct($url = null,array $data = null)
    {
        //require 'IMConfig.php';
        //var_dump(hash('sha1', $this->config['AppSecret'].'269e7c1d71574d038fc0de2f5138be01'.'1495117713'));
        $this->getNonce();
        $this->getTime();
        $this->setHeader();
        $this->setBody($data);
        $this -> ch = curl_init();
        $this->setopt($url);
        $this->exec();
    }

    private function setBody($data){
        foreach ($data as $key => $value) {
            $this->body .= $key . '=' . $value . '&';
        }
//
//        if(isset($data['accid'])){
//            $this->body .= 'accid=' . $data['accid'];
//        }
//
//        if(isset($data['name'])){
//            $this->body .= '&name=' . $data['name'];
//        }
//
//        if(isset($data['props'])){
//            $this->body .= '&props=' . $data['props'];
//        }
//
//        if(isset($data['icon'])){
//            $this->body .= '&icon=' . $data['icon'];
//        }
//
//        if(isset($data['token'])){
//            $this->body .= '&token=' . $data['token'];
//        }
    }

    private function setHeader(){
        array_push($this->header,'Content-Type:application/x-www-form-urlencoded;charset=utf-8');
        array_push($this->header,'AppKey:' . $this->config['AppKey']);
        array_push($this->header,'Nonce:' . $this->nonce);
        array_push($this->header,'CurTime:' . $this->curTime);
        array_push($this->header,'CheckSum:' . $this->getCheckSum());
    }

    public function getHeader(){
        return $this -> header;
    }

    private function getTime(){
        $this->curTime = time();
    }

    private function getNonce(){
        $this->nonce = Common::getRand();
    }

    private function getCheckSum(){
        $value = $this->config['AppSecret'] . $this->nonce . $this->curTime;
        if($value == null){
            return null;
        }
        else{
            return hash('sha1', $value);
        }
        //return self::encode("sha1", $appSecret + $nonce + $curTime);
    }

    private function setopt($url){
        curl_setopt($this -> ch,CURLOPT_URL,$url);
        curl_setopt($this -> ch,CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($this -> ch,CURLOPT_RETURNTRANSFER,true);
        //curl_setopt($this -> ch,CURLOPT_HEADER, true);              //响应头
        //var_dump($this->header);
        curl_setopt($this -> ch,CURLOPT_HTTPHEADER,$this->header);
        curl_setopt($this -> ch,CURLOPT_POST,true);
        curl_setopt($this -> ch,CURLOPT_TIMEOUT,60);
        //curl_setopt($this -> ch,CURLOPT_POSTFIELDS,$data);


        //$querys = "";
        curl_setopt($this -> ch, CURLOPT_POSTFIELDS, $this->body);
        //var_dump(curl_exec($curl));
    }

    private function exec(){
        $this -> response = curl_exec($this->ch);
        //var_dump(curl_getinfo($this->ch));

        $this -> close();
    }

    private function close(){
        curl_close($this->ch);
    }

    public function getResponse(){
        return $this -> response;
    }

    public function test2(){
        /**
         ** @desc 封装 curl 的调用接口，post的请求方式
         **/
        function doCurlPostRequest($url,$requestString,$timeout = 5){
            if($url == '' || $requestString == '' || $timeout <=0){
                return false;
            }
            $con = curl_init((string)$url);
            curl_setopt($con, CURLOPT_HEADER, false);
            curl_setopt($con, CURLOPT_POSTFIELDS, $requestString);
            curl_setopt($con, CURLOPT_POST,true);
            curl_setopt($con, CURLOPT_RETURNTRANSFER,true);
            curl_setopt($con, CURLOPT_TIMEOUT,(int)$timeout);
            return curl_exec($con);
        }
    }
}